﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Ramp.Service.Contract;
using System.IO;

namespace RampServiceTester
{
    class RampTester
    {        
        static void Main(string[] args)
        {
            ConsoleKeyInfo cki = new ConsoleKeyInfo();
            string serviceUri = "net.tcp://localhost:9898/RampService";
            NetTcpBinding tcpBinding = new NetTcpBinding();
            tcpBinding.TransactionFlow = false;
            tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
            tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            tcpBinding.Security.Mode = SecurityMode.None;

            EndpointAddress endPointAddress = new EndpointAddress(serviceUri);

            Console.WriteLine("Attempt to connect to: " + serviceUri);
                        
            ChannelFactory<IRampService> rampChannelFactory = new ChannelFactory<IRampService>(tcpBinding, endPointAddress);

            IRampService proxy = rampChannelFactory.CreateChannel();
            
            if (proxy != null)
            {
                Console.WriteLine("Got the Proxy");
                do
                {
                    Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");
                    while (Console.KeyAvailable == false)
                    {
                        System.Threading.Thread.Sleep(1000); // Loop until input is entered.
                        Console.WriteLine("Message from server: " + proxy.GetLoadTestRunningStatus());
                    }
                    cki = Console.ReadKey(true);
                    Console.WriteLine("You pressed the '{0}' key.", cki.Key);
                } while (cki.Key != ConsoleKey.X);                
            }
            else
            {
                Console.WriteLine("Unable to get Proxy");
            }

            Console.WriteLine("Closing Channel and Proxy");
            if (proxy != null)
            {
                ((IClientChannel)proxy).Close();
                rampChannelFactory.Close();
            }

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
 
    }
}
